package com.atguigu.service.impl;

import com.atguigu.dao.UpdateTomcatCodeAndHeartbeatTaskDao;
import com.atguigu.dao.impl.UpdateTomcatCodeAndHeartbeatTaskDaoImpl;
import com.atguigu.pojo.Page;
import com.atguigu.pojo.UpdateTomcatCodeAndHeartbeatTask;
import com.atguigu.service.UpdateTomcatCodeAndHeartbeatTaskService;

import java.util.List;

public class UpdateTomcatCodeAndHeartbeatTaskServiceImpl implements UpdateTomcatCodeAndHeartbeatTaskService {
    UpdateTomcatCodeAndHeartbeatTaskDao updateTomcatCodeAndHeartbeatTaskDao=new UpdateTomcatCodeAndHeartbeatTaskDaoImpl();
    @Override
    public void addUpdateTomcatCodeAndHeartbeatTask(UpdateTomcatCodeAndHeartbeatTask updateTomcatCodeAndHeartbeatTask) {
        updateTomcatCodeAndHeartbeatTaskDao.addUpdateTomcatCodeAndHeartbeatTask(updateTomcatCodeAndHeartbeatTask);
    }

    @Override
    public void updateUpdateTomcatCodeAndHeartbeatTask(UpdateTomcatCodeAndHeartbeatTask updateTomcatCodeAndHeartbeatTask) {
        updateTomcatCodeAndHeartbeatTaskDao.updateUpdateTomcatCodeAndHeartbeatTask(updateTomcatCodeAndHeartbeatTask);
    }

    @Override
    public void deleteUpdateTomcatCodeAndHeartbeatTaskById(String id) {
        updateTomcatCodeAndHeartbeatTaskDao.deleteUpdateTomcatCodeAndHeartbeatTaskById(id);
    }

    @Override
    public UpdateTomcatCodeAndHeartbeatTask queryUpdateTomcatCodeAndHeartbeatTaskById(String id) {
        UpdateTomcatCodeAndHeartbeatTask updateTomcatCodeAndHeartbeatTask=updateTomcatCodeAndHeartbeatTaskDao.queryUpdateTomcatCodeAndHeartbeatTaskById(id);
        return updateTomcatCodeAndHeartbeatTask;
    }


    @Override
    public List<UpdateTomcatCodeAndHeartbeatTask> queryUpdateTomcatCodeAndHeartbeatTasks() {
        List<UpdateTomcatCodeAndHeartbeatTask> updateTomcatCodeAndHeartbeatTasks=updateTomcatCodeAndHeartbeatTaskDao.queryUpdateTomcatCodeAndHeartbeatTasks();
        return updateTomcatCodeAndHeartbeatTasks;
    }

    @Override
    public Page page(Integer pageNo, Integer pageSize) {
        Page<UpdateTomcatCodeAndHeartbeatTask> page=new Page<>();
        page.setPagesize(pageSize);
        Integer pageTotalCount=updateTomcatCodeAndHeartbeatTaskDao.queryForPageTotalCount();
        page.setPageTotalCount(pageTotalCount);
        Integer pageTotal=pageTotalCount/pageSize;
        if(pageTotalCount % pageSize >0){
            pageTotal++;//总条数,除以一页条数,大于0,页码数+1
        }
        page.setPageTotal(pageTotal);
        System.out.println("pageNo 原来是 is:"+pageNo);
        if(pageNo<1){//防止用户输入量太大
            pageNo=1;
        }else if(pageNo>pageTotal){
            pageNo=pageTotal;
        }
        page.setPageNo(pageNo);//当前页码
        System.out.println("pageNo 现在是 is:"+pageNo);
        System.out.println("pageTotal is:"+pageTotal);
        //要得到的是page的items
        Integer begin=(page.getPageNo()-1)*pageSize;
        List<UpdateTomcatCodeAndHeartbeatTask> items=updateTomcatCodeAndHeartbeatTaskDao.queryForPageItems(begin,pageSize);
        page.setItems(items);
        return page;
    }
}
